﻿@model HCQ2_Model.T_Role
@{
    ViewBag.Title = "价格管理";
}
@section headerSection{
    <style>
        #exampleToolbar > .form-group {
            float: left;
            margin: 0;
        }
    </style>
}
<div class="animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins" style="margin-bottom: 0px;">
        <div class="ibox-content" style="padding: 5px 20px 0px 20px;border:0;">
            <div class="row row-lg">
                <div class="col-sm-12 col-sm-leftRight">
                    <!-- Example Toolbar -->
                    <div class="example">
                        <div class="btn-group hidden-xs" id="exampleToolbar" role="group">
                            <div class="form-group">
                                <button id="btnRepertoryBatch" class="btn btn-primary" type="button"><i class="fa fa-group"></i>&nbsp;批量保存</button>
                            </div>
                        </div>
                        <table id="exampleTableToolbar" data-mobile-responsive="true" data-radio="true"></table>
                    </div>
                    <!-- End Example Toolbar -->
                </div>
            </div>
        </div>
    </div>
    <!-- End Panel Other -->
</div>
@section footSection{
    <script>
        var options, $table,  fileId;
        var priceManager = {
            initPage: function () {
                priceManager.initData();
                priceManager.initTable();
                priceManager.bindEvent();
            },
            initData: function () {
                fileId = $.getUrls("fileId");
            },
            //初始化Table
            initTable: function () {
                options = {
                    url: ctx + "/VRSpecification/InitPriceTable",
                    cutHeight: 25,
                    toolbar: "#exampleToolbar",
                    uniqueId: "keyCode",//主键id
                    queryParams: function (params) {
                        params = {
                            fileId: fileId
                        }
                        return params;
                    },
                    columns: [
                        {
                            field: '', //第一列序号
                            title: '序号',
                            align: 'center',
                            width: 50,
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },{
                            field: 'priceName',
                            title: '规格组合',
                            align: 'center',
                            formatter: function (value, row, index) {
                                if (!value)
                                    return "空";
                                return value;
                            }
                        }, {
                            field: 'specMoney',
                            title: '价格',
                            align: 'center',
                            formatter: function (value, row, index) {
                                var input = "<input id=\"" + row.keyCode + "\" type=\"text\" value=\"" + value +"\" onblur=\"blurfloat2(this)\" onkeyup=\"help.float2(this)\" style=\"display:inline-block;width:250px;\"  class=\"form-control repertoryNum\" placeholder=\"请输入金额\">&nbsp;&nbsp;";
                                input += "<button class=\"btn btn-info btn-circle btn-outline\" title=\"编辑\" onclick=\"priceManager.specEdit('" + row.keyCode + "')\" type=\"button\"><i class=\"fa fa-pencil\"></i></button>&nbsp;";
                                return input;
                            }
                        }]
                }
                $table = tableHelper.initTable("exampleTableToolbar", options);
            },
            //单个编辑库存
            specEdit: function (keyCode) {
                var param = [{
                    'keyCode': keyCode,
                    'specMoney': $('#' + keyCode).val()
                }];
                priceManager.editRepertoryData(param);
            },
            //保存库存数据
            editRepertoryData: function (param) {
                var index = layer.msg("数据处理中...", { icon: 6, time: 9000 });
                $.ajax({
                    type: 'post',
                    url: ctx + '/VRSpecification/BatchPriceEdit',
                    dataType: 'json',
                    data: { param: param },
                    async: false,
                    success: function (data) {
                        layer.close(index);
                        if (data.Statu === 0) {
                            layer.msg(data.Msg);
                            $table.bootstrapTable('refresh', options);
                        }
                        else
                            layer.msg(data.Msg, { icon: 5 });
                    }
                });
            },
            //绑定事件
            bindEvent: function () {
                $('#btnRepertoryBatch').on('click', function () {
                    var list = $('.repertoryNum');
                    var param = [];
                    $.each(list, function (index, item) {
                        param.push({
                            'keyCode': $(item).attr('id'),
                            'specMoney': $(item).val()
                        });
                    });
                    if (param == null || param.length <= 0) {
                        layer.msg("数据为空！", { icon: 5 });
                        return false;
                    }
                    priceManager.editRepertoryData(param);
                });
            }
        }
        $(function () {
            priceManager.initPage();
        });
        function blurfloat2(htmlId) {
            var val = $(htmlId).val();
            if (val && val.length > 1) {
                var f = val.substring(0, 1),
                    s = val.substring(1, 2),
                    l = val.substring(val.length-1);
                if (f === "0" && s != ".")
                    $(htmlId).val(val.substring(1));
                //if (l === ".")
                //    $(htmlId).val(val.substring(0, val.length-1));
            }
        }
    </script>
}
